package cn.itcast.test;

import cn.itcast.documentapis.esClient;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO :
 * @Author: zj
 * @Date: 15:55 2019/3/14
 * @Version 1.0
 **/
public class testApi {

    @Test
    public void searchTime() {
        TransportClient client = esClient.getClient();

        Map<String, String> matchPhrase = new HashMap<String, String>();
        matchPhrase.put("jmID", "33333");
        matchPhrase.put("xxID", "12333");

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        for (Map.Entry<String, String> entry : matchPhrase.entrySet()) {
            queryBuilder.must(QueryBuilders.matchQuery(entry.getKey(), entry.getValue()));
        }
            queryBuilder.must(QueryBuilders.rangeQuery("dattime")

                .gte("2018-01-01")
                .lte("2018-01-14")
        );
        SearchRequestBuilder pomssynlog = client.prepareSearch("pomssynlog")
                .setFrom(1)
                .setSize(3);


        SearchResponse searchResponse = pomssynlog.get();
        System.out.println(searchResponse.getHits().getTotalHits());
        // System.out.println(pomssynlog.toString());

        List<Map<String, Object>> histoVos = new LinkedList<Map<String, Object>>();

        for (SearchHit hit : searchResponse.getHits()) {
            System.out.println(hit.getId());
            //System.out.println(hit);
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            if (!sourceAsMap.isEmpty()) {
                histoVos.add(sourceAsMap);
            }
        }
        histoVos.forEach(System.out::println);
    }

    @Test
    public void tests() {
        String requestId = "";
        if (requestId !=null && !requestId.equals("")) {
            System.out.println("不为空");
        }else {
            System.out.println("为空");
        }


    }
}
